<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.packing_delete" @click="handleDelete">删 除 </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from '@/api/warehouse/packing';
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: '工厂， 仓库 mes_site.ID',
            prop: 'siteId',
            rules: [
              {
                required: true,
                message: '请输入工厂， 仓库 mes_site.ID',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装条码，序列号',
            prop: 'sequenceNumber',
            rules: [
              {
                required: true,
                message: '请输入包装条码，序列号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '父级包装ID',
            prop: 'parentPackId',
            rules: [
              {
                required: true,
                message: '请输入父级包装ID',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '拆包前包装',
            prop: 'originalPackId',
            rules: [
              {
                required: true,
                message: '请输入拆包前包装',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '物料编码ID',
            prop: 'partMasterId',
            rules: [
              {
                required: true,
                message: '请输入物料编码ID',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '供应商编码',
            prop: 'vendorCode',
            rules: [
              {
                required: true,
                message: '请输入供应商编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '实际包装数量（产品数量）',
            prop: 'quantity',
            rules: [
              {
                required: true,
                message: '请输入实际包装数量（产品数量）',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包规数量, 标准包装数量',
            prop: 'packQuantitySpec',
            rules: [
              {
                required: true,
                message: '请输入包规数量, 标准包装数量',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装类型, 原料， MATERIAL,  成品， PRODUCT',
            prop: 'packType',
            rules: [
              {
                required: true,
                message: '请输入包装类型, 原料， MATERIAL,  成品， PRODUCT',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装子类型，FIRST: 首件, LAST：尾件, GENERAL：普通',
            prop: 'packSubType',
            rules: [
              {
                required: true,
                message: '请输入包装子类型，FIRST: 首件, LAST：尾件, GENERAL：普通',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '描述',
            prop: 'description',
            rules: [
              {
                required: true,
                message: '请输入描述',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '批次，先进先出以这个为条件判断。该字段更名为物料批次，因为收料按批次进行，批次的生成又可以按收料日期与生产日期进行转换',
            prop: 'lotNo',
            rules: [
              {
                required: true,
                message: '请输入批次，先进先出以这个为条件判断。该字段更名为物料批次，因为收料按批次进行，批次的生成又可以按收料日期与生产日期进行转换',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '制造批次生产日期',
            prop: 'lotDate',
            rules: [
              {
                required: true,
                message: '请输入制造批次生产日期',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '供应商批次',
            prop: 'vendorLotNo',
            rules: [
              {
                required: true,
                message: '请输入供应商批次',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '生产日期',
            prop: 'vendorDate',
            rules: [
              {
                required: true,
                message: '请输入生产日期',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '接收日期',
            prop: 'receiveDate',
            rules: [
              {
                required: true,
                message: '请输入接收日期',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '层级',
            prop: 'level',
            rules: [
              {
                required: true,
                message: '请输入层级',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '是否有外包装',
            prop: 'hasOutpacking',
            rules: [
              {
                required: true,
                message: '请输入是否有外包装',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装人',
            prop: 'packedBy',
            rules: [
              {
                required: true,
                message: '请输入包装人',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装日期',
            prop: 'packedDate',
            rules: [
              {
                required: true,
                message: '请输入包装日期',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '是否需要称重',
            prop: 'weightRequired',
            rules: [
              {
                required: true,
                message: '请输入是否需要称重',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装重量',
            prop: 'packWeight',
            rules: [
              {
                required: true,
                message: '请输入包装重量',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '毛重',
            prop: 'grossWeight',
            rules: [
              {
                required: true,
                message: '请输入毛重',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '是否尾箱',
            prop: 'isTailBox',
            rules: [
              {
                required: true,
                message: '请输入是否尾箱',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '是否封箱',
            prop: 'isPackFinished',
            rules: [
              {
                required: true,
                message: '请输入是否封箱',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '是否强制包装，允许未达包规数而强制包装',
            prop: 'isForcePack',
            rules: [
              {
                required: true,
                message: '请输入是否强制包装，允许未达包规数而强制包装',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark',
            rules: [
              {
                required: true,
                message: '请输入备注',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '存储期限',
            prop: 'storageTime',
            rules: [
              {
                required: true,
                message: '请输入存储期限',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '超期日期',
            prop: 'expirationDate',
            rules: [
              {
                required: true,
                message: '请输入超期日期',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '成品包规配置详情，对应mes_product_packing_detail.ID',
            prop: 'productPackingDetailId',
            rules: [
              {
                required: true,
                message: '请输入成品包规配置详情，对应mes_product_packing_detail.ID',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '收料记录ID, wh_receiving.id',
            prop: 'receivingId',
            rules: [
              {
                required: true,
                message: '请输入收料记录ID, wh_receiving.id',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '卡板包装条码，和pallet一对多关系，可以有pallet也可以没有',
            prop: 'palletId',
            rules: [
              {
                required: true,
                message: '请输入卡板包装条码，和pallet一对多关系，可以有pallet也可以没有',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '标签生成记录id， mes_sequence_generation.ID',
            prop: 'sequenceGenerationId',
            rules: [
              {
                required: true,
                message: '请输入标签生成记录id， mes_sequence_generation.ID',
                trigger: 'blur'
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.packing_add, false),
        viewBtn: this.vaildData(this.permission.packing_view, false),
        delBtn: this.vaildData(this.permission.packing_delete, false),
        editBtn: this.vaildData(this.permission.packing_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>
